home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1996 June
/
EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso
/
earcd
/
dtype
/
fontdt12.lha
/
Src
/
prefs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-05-06
|
5KB
|
171 lines
/*
** prefs.c - prefrences handling for Font DataType class
** Copyright © 1995-96 Michael Letowski
*/
#include <exec/types.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <support/types.h>
#include <support/dos.h>
#include <stdlib.h>
#include "classbase.h"
#include "prefs.h"
#include <proto/exec.h>
#include <proto/dos.h>
/*
** Private constants and macros
*/
#define BUF_SIZE 1024 /* Size of prefs file buffer */
#define MAX_SUB_CNT 3 /* Max number of sub-options */
#define PREFS_PATH1 "PROGDIR:Prefs/DataTypes/font.prefs"
#define PREFS_PATH2 "ENV:DataTypes/font.prefs"
/*
** Private functions prototypes
*/
STATIC LBOOL ReadPrefs(struct ClassBase *cb, struct PrefsHandle *ph,
struct Opts *opts, STRPTR name);
STATIC LBOOL ReadSubPrefs(struct ClassBase *cb, struct RDArgs *rda,
STRPTR argStr, STRPTR template,
LONG fields[], LONG optCnt);
STATIC struct RDArgs *ReadArgsBuf(struct ClassBase *cb, STRPTR template,
LONG *opts, struct RDArgs *rda);
/*
** Public functions
*/
struct PrefsHandle *GetFontPrefs(struct ClassBase *cb, struct Opts *opts)
{
struct PrefsHandle *PH;
LBOOL Res=FALSE;
clear(opts); /* Set defaults */
try(new(PH,MEMF_CLEAR), NO_PREFS);
try(PH->ph_RDA1=AllocDosObject(DOS_RDARGS,NULL), NO_RDA1);
try(PH->ph_RDA2=AllocDosObject(DOS_RDARGS,NULL), NO_RDA2);
if(ThisProcessS->pr_HomeDir) /* PROGDIR: exists? */
Res=ReadPrefs(cb,PH,opts,PREFS_PATH1); /* Try first path */
unless(Res) /* Args not read so far */
ReadPrefs(cb,PH,opts,PREFS_PATH2); /* Try second path */
return(PH);
catch(FOO, );
catch(NO_RDA2, FreeDosObject(DOS_RDARGS,PH->ph_RDA2));
catch(NO_RDA1, FreeDosObject(DOS_RDARGS,PH->ph_RDA1));
catch(NO_PREFS, delete(PH));
return(NULL);
} /* GetFontPrefs */
VOID FreeFontPrefs(struct ClassBase *cb, struct PrefsHandle *ph)
{
FreeArgs(ph->ph_Args); /* Free args (may be NULL) */
FreeDosObject(DOS_RDARGS,ph->ph_RDA2); /* Free 2nd RDA structure */
FreeDosObject(DOS_RDARGS,ph->ph_RDA1); /* Free 1st RDA structure */
delete(ph);
} /* FreeFontPrefs */
/*
** Private functions
*/
STATIC LBOOL ReadPrefs(struct ClassBase *cb, struct PrefsHandle *ph,
struct Opts *opts, STRPTR name)
{
struct RDArgs *RDA1=ph->ph_RDA1;
struct RDArgs *RDA2=ph->ph_RDA2;
STRPTR Buf,Buffer;
LONG Len,Size;
BPTR FH;
try(FH=Open(name,MODE_OLDFILE), NO_FILE);
try(Buffer=AllocMem(BUF_SIZE,MEMF_PUBCLR), NO_BUF);
/* Consolidate multiple lines into a single buffer */
Buf=Buffer;
Size=BUF_SIZE-1;
while(Size>0 && FGets(FH,Buf,Size))
{
Len=strlen(Buf);
if(Buf[Len-1]=='\n') Buf[Len-1]=' ';
Buf+=Len;
Size-=Len;
}
/* Set up all RDA structures */
RDA1->RDA_Source.CS_Buffer=Buffer; /* Set up buffer to read from */
RDA2->RDA_Source.CS_Buffer=Buffer; /* Set up buffer to read from */
/* Read args */
if(ph->ph_Args=ReadArgsBuf(cb,TEMPLATE,(LONG *)opts,RDA1))
{
opts->opt_DPIFlag=
ReadSubPrefs(cb,RDA2,opts->opt_DPIStr,TEMPLATEDPI,&opts->opt_XDPI,2);
opts->opt_ForeFlag=
ReadSubPrefs(cb,RDA2,opts->opt_ForeStr,TEMPLATECOL,opts->opt_ForeCol,3);
opts->opt_BackFlag=
ReadSubPrefs(cb,RDA2,opts->opt_BackStr,TEMPLATECOL,opts->opt_BackCol,3);
}
catch(ERROR, );
catch(NO_BUF, FreeMem(Buffer,BUF_SIZE));
catch(NO_FILE, Close(FH));
return(FH ? TRUE : FALSE); /* True if prefs file exists */
} /* ReadPrefs */
STATIC LBOOL ReadSubPrefs(struct ClassBase *cb, struct RDArgs *rda,
STRPTR argStr, STRPTR template,
LONG fields[], LONG optCnt)
{
LONG *SubOpts[MAX_SUB_CNT];
struct RDArgs *Args;
LONG I;
LBOOL Res=FALSE;
if(argStr)
{
strcpy(rda->RDA_Source.CS_Buffer,argStr);
if(Args=ReadArgsBuf(cb,template,(LONG *)SubOpts,rda))
{
Res=TRUE;
for(I=0; I<optCnt; I++)
if(SubOpts[I])
fields[I]=*SubOpts[I];
FreeArgs(Args);
}
}
return(Res);
} /* ReadSubPrefs */
STATIC struct RDArgs *ReadArgsBuf(struct ClassBase *cb, STRPTR template,
LONG *opts, struct RDArgs *rda)
{
STRPTR Buffer=rda->RDA_Source.CS_Buffer;
LONG Len;
/* Prepare buffer */
Len=strlen(Buffer); /* Get size of buffer */
Buffer[Len]='\n'; /* New line char */
Buffer[Len+1]='\0'; /* String terminator */
/* Prepare RDArgs */
rda->RDA_Source.CS_Length=Len+1; /* Set up string len */
rda->RDA_Source.CS_CurChr=0; /* Set up current char */
rda->RDA_Buffer=NULL; /* As documented in AutoDocs */
fset(rda->RDA_Flags,RDAF_NOPROMPT); /* Disable prompts */
return(ReadArgs(template,opts,rda)); /* Read arguments */
} /* ReadArgsBuf */